package babel;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;



public class GUIEingabe extends JFrame{

	
	private JTextField textFileName = new JTextField("");
	private JButton buttonBrowse = new JButton("Browse");
	private JButton buttonStart = new JButton("Start");
	private JTextField AnzahlStellen = new JTextField("1");
	private JPanel Panel1 = new JPanel();
	private JPanel Panel2 = new JPanel(); 
	private JPanel Panel3 = new JPanel();
	private JPanel Panel4 = new JPanel();
	private static JTextArea TextAreaOriginal = new JTextArea(); 
	private static JTextArea TextAreaAusgabe = new JTextArea();
	private JScrollPane scrollPane = new JScrollPane(TextAreaOriginal);
	
	
	private final JFileChooser fileChooser = new JFileChooser();
	
	public static void main(String[] args) {
		new GUIEingabe();
	
		// TODO Auto-generated method stub

	}
	private GUIEingabe(){
//		this.setSize(620, 600);
		this.setTitle("BABBLE!");
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		
		
		
		
		zeichneGUI();
		addEventHandlers();

		// All done, show the application
		this.pack();
		this.setVisible(true); 
		
		 
	}
	
	private void zeichneGUI(){
//		
		Container content = this.getContentPane();
		content.setLayout(new FlowLayout());
	
		Border whiteSpaceBorder = new EmptyBorder(5, 5, 5, 5);

		content.add(Panel3);
		content.add(Panel4);
		Panel4.add(Panel2);
		Panel4.add(Panel1);
		
		
//		Panel1.setBackground(Color.BLUE);
		Panel1.setLayout(new GridLayout(1,3));
//		Panel1.setBorder(whiteSpaceBorder);
		Panel1.add(new JLabel("Window size"));
		AnzahlStellen.setPreferredSize(new Dimension(50, 0));
		AnzahlStellen.setMaximumSize(new Dimension(50, 50));
//		AnzahlStellen.setHorizontalAlignment(JTextField.CENTER);
		Panel1.add(AnzahlStellen);
		Panel1.add(buttonStart);
		
		Panel2.setLayout(new GridLayout(1,3));
		Panel2.setBorder(whiteSpaceBorder);
		Panel2.add(new JLabel("Input file"));
//		Panel2.setBackground(Color.RED);
		textFileName.setPreferredSize(new Dimension(100, 0));
//		textFileName.setBackground(Color.BLACK);
		Panel2.add(textFileName);
//		Panel2.add(Box.createHorizontalStrut(5));
		Panel2.add(buttonBrowse);

		// Bottom box - the "analyze" button
//		JPanel bottomBox = Box.createHorizontalBox();
//		content.add(bottomBox);
//		bottomBox.setBorder(whiteSpaceBorder);
//		bottomBox.add(Box.createHorizontalGlue());
		
		
		
		
//		Panel3.setLayout(new BorderLayout());
		Panel3.add(TextAreaOriginal, BorderLayout.WEST);
		Panel3.add(TextAreaAusgabe, BorderLayout.EAST);
		
		Panel4.setLayout(new GridLayout(2,1));
		
		TextAreaOriginal.setPreferredSize(new Dimension(300, 400));
//	    JScrollPane scrollPane = new JScrollPane(TextAreaOriginal, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
//	        JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
	    TextAreaAusgabe.setPreferredSize(new Dimension(300, 400));
//	    JScrollPane scrollPane2 = new JScrollPane(TextAreaAusgabe, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
//	        JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
	    TextAreaOriginal.setLineWrap(true);
	    TextAreaOriginal.setWrapStyleWord(true);
	    TextAreaAusgabe.setLineWrap(true);
	    TextAreaAusgabe.setWrapStyleWord(true);
	    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
	    TextAreaOriginal.add(scrollPane);
	    
	    
	}
	
	private void chooseInputFile() {
		int result = fileChooser.showOpenDialog(this);

    if (result == JFileChooser.APPROVE_OPTION) {
        File file = fileChooser.getSelectedFile();
        textFileName.setText(file.getAbsolutePath());
    }	}
	
	private void addEventHandlers() {
		// The file-browser
		buttonBrowse.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent evt) {
				chooseInputFile();
			}
		});
		buttonStart.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent evt) {
				try {
					Start();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
	
	private File checkInputFile(String fileName) {
		boolean allOk = false;
		File fileIn = new File(fileName);
		
		if (!fileIn.exists()) {
			JOptionPane.showMessageDialog(this, "File does not exist", "File error", JOptionPane.ERROR_MESSAGE);
		} else if (!fileIn.isFile()) {
			JOptionPane.showMessageDialog(this, "Must be a file, not a directory", "File error", JOptionPane.ERROR_MESSAGE);
		} else if (!fileIn.canRead()) {
			JOptionPane.showMessageDialog(this, "File is not readable", "File error", JOptionPane.ERROR_MESSAGE);
		} else {
			allOk = true;
		}
		
		if (!allOk) fileIn = null;
		
		return fileIn;
	}
	private int checkAnzahlStellen(String StringStellen) {
		int Anzahlstellen = Integer.parseInt(StringStellen);
		return Anzahlstellen;	
		
	}
	
	private void Start() throws IOException{
		String fileName = textFileName.getText();
		File inputFile = checkInputFile(fileName);
		 int stellen = checkAnzahlStellen(AnzahlStellen.getText().toString());
//		System.out.println(stellen);
		
		if (inputFile != null & stellen > 0) {
//			System.out.println(fileName);
			Original Ori = new Original(inputFile);
			Ori.kopiereOriginal();
		}
	}
	static void setTextOriginal(String Originaltext){
		TextAreaOriginal.setText(Originaltext);
	}
	static void setTextAusgabe(String Originaltext){
		TextAreaAusgabe.setText(Originaltext);
	}
	}

	

